package com.time.hellotime.components.dynamics;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.time.hellotime.R;
import com.time.hellotime.base.BaseViewHolder;
import com.time.hellotime.base.BaseXRecAdapter;
import com.time.hellotime.db.model.PicModel;
import com.time.hellotime.utils.ScreenUtils;
import com.time.hellotime.utils.ToastUtils;
import com.time.hellotime.viewmodel.DynamicsViewModel;
import com.time.hellotime.views.WrapHeightGridView;
import com.time.hellotime.webserver.helper.picasso.PicassoImageLoader;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.api.widget.Widget;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bing on 2017/7/12.
 * E-mail is tx16bing@163.com
 */

class DynamicsAdapter extends BaseXRecAdapter<DynamicsViewModel> {

    public DynamicsAdapter(Context mContext, List<DynamicsViewModel> list, int viewTypeCount) {
        super(mContext, list, viewTypeCount);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View convertView = inflater.inflate(R.layout.item_dynamics_list, parent, false);
        DynamicsViewHolder publishViewHolder = new DynamicsViewHolder(convertView);
        convertView.setTag(publishViewHolder);
        return publishViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position, boolean isItem) {
        final DynamicsViewModel dynamics = getItem(position);
        final int viewType = getAdapterItemViewType(position);

        final DynamicsViewHolder publishHolder = (DynamicsViewHolder) holder;
        if (!TextUtils.isEmpty(dynamics.getUserHead())) {
            int sizeUser = ScreenUtils.dip2px(52);
            PicassoImageLoader.loadImageViewSize(mContext,
                    dynamics.getUserHead(),
                    sizeUser,
                    sizeUser,
                    R.drawable.iv_default_user_52,
                    R.drawable.iv_default_user_52,
                    toString(), publishHolder.rivHeadImg);

            publishHolder.rivHeadImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, dynamics, viewType);
                }
            });
        }

        if (position == getItemCount() - 1) {
            publishHolder.divider.setVisibility(View.GONE);
        } else {
            publishHolder.divider.setVisibility(View.VISIBLE);
        }

        publishHolder.tvNick.setText(dynamics.getShowName());
        publishHolder.tvNick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, position, dynamics, viewType);
            }
        });

        publishHolder.tvTime.setText(dynamics.getShowTime());

        ViewTreeObserver.OnPreDrawListener pl = new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                ViewTreeObserver obs = publishHolder.tvContent.getViewTreeObserver();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    obs.removeOnPreDrawListener(this);
                }
                int count = publishHolder.tvContent.getLineCount();
                if (count > 5) {
                    publishHolder.tvContent.setMaxLines(5);
                    publishHolder.tvShowHide.setVisibility(View.VISIBLE);
                } else {
                    publishHolder.tvContent.setMaxLines(100);
                    publishHolder.tvShowHide.setVisibility(View.GONE);
                }
                return true;
            }
        };

        if (!TextUtils.isEmpty(dynamics.getContent())) {
            publishHolder.tvContent.setText(dynamics.getContent());
            publishHolder.tvContent.setVisibility(View.VISIBLE);
            ViewTreeObserver vto = publishHolder.tvContent.getViewTreeObserver();
//                vto.addOnGlobalLayoutListener(gl);
            vto.addOnPreDrawListener(pl);
        } else {
            publishHolder.tvContent.setVisibility(View.GONE);
            publishHolder.tvShowHide.setVisibility(View.GONE);
        }

        publishHolder.tvShowHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.equals(mContext.getString(R.string.publish_show_all),
                        publishHolder.tvShowHide.getText().toString())) {
                    // 如果是显示全文，点击显示全部，并且修改按钮内容为收起
                    publishHolder.tvShowHide.setText(mContext.getString(R.string.publish_hide_all));
                    publishHolder.tvContent.setMaxLines(100);
                } else {
                    publishHolder.tvShowHide.setText(mContext.getString(R.string.publish_show_all));
                    publishHolder.tvContent.setMaxLines(5);
                }
            }
        });

        if (TextUtils.equals(dynamics.getCommentNum(), "0")) {
            publishHolder.tvComment.setText("");
            publishHolder.tvComment.setVisibility(View.GONE);
        } else {
            publishHolder.tvComment.setVisibility(View.VISIBLE);
            publishHolder.tvComment.setText(dynamics.getCommentNum());
        }

        if (TextUtils.equals(dynamics.getLikeNum(), "0")) {
            publishHolder.tvLike.setText("");
            publishHolder.tvLike.setVisibility(View.GONE);
        } else {
            publishHolder.tvLike.setText(dynamics.getLikeNum());
            publishHolder.tvLike.setVisibility(View.VISIBLE);
        }

        if (dynamics.isLike()) {
            publishHolder.ivLike.setImageResource(R.drawable.iv_like_yes);
        } else {
            publishHolder.ivLike.setImageResource(R.drawable.iv_like_no);
        }

        publishHolder.ivLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dynamics.isLike()) {
                    ToastUtils.showShort("你已经赞过了");
                } else {
                    dynamics.setLike(true);
                    publishHolder.ivLike.setImageResource(R.drawable.iv_like_yes);
                    if (TextUtils.isEmpty(dynamics.getDynamicsId())) {
                        return;
                    } else {
                        onItemClickListener.onItemClick(v, position, dynamics, viewType);
                    }
                }
            }
        });

        if (dynamics.getPics().isEmpty()) {
            publishHolder.gvPics.setVisibility(View.GONE);
        } else {
            int size = dynamics.getPics().size();
            if (size < 3){
                publishHolder.gvPics.setNumColumns(size);
            }else {
                publishHolder.gvPics.setNumColumns(3);
            }
            publishHolder.gvPics.setVisibility(View.VISIBLE);
            PicAdapter picAdapter = new PicAdapter(mContext, dynamics.getPics());
            publishHolder.gvPics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    List<String> galleryImages = new ArrayList<>();
                    for (PicModel model : dynamics.getPics()){
                        galleryImages.add(model.getPicAddr());
                    }
                    GalleryActivity.start(((Activity)mContext), 11, galleryImages, position, 80);
//                    Album.gallery(mContext)
//                            .requestCode(2)
//                            .checkedList((ArrayList<String>) galleryImages)
//                            .navigationAlpha(80)
//                            .checkable(false) // 是否有浏览时的选择功能。
//                            .currentPosition(position)
//                            .widget(Widget.newLightBuilder(mContext)
//                                    .title("已选")
//                                    .mediaItemCheckSelector(Color.WHITE, Color.GREEN)
//                                    .bucketItemCheckSelector(
//                                            ContextCompat.getColor(mContext, R.color.colorPrimary),
//                                            ContextCompat.getColor(mContext, R.color.colorPrimary))
//                                    .build())
//                            .start();
                }
            });
            publishHolder.gvPics.setAdapter(picAdapter);
        }

        publishHolder.tvLocation.setText(TextUtils.ellipsize(
                dynamics.getLocation(),
                publishHolder.tvLocation.getPaint(),
                ScreenUtils.getDisplayWidth() * 2 / 5,
                TextUtils.TruncateAt.END));
        publishHolder.ivShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    if (TextUtils.isEmpty(dynamics.getDynamicsId())) {
                        return;
                    } else {
                        onItemClickListener.onItemClick(v, position, dynamics, viewType);
                    }
                }
            }
        });

        publishHolder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    if (TextUtils.isEmpty(dynamics.getDynamicsId())) {
                        return;
                    } else {
                        onItemClickListener.onItemClick(v, position, dynamics, viewType);
                    }
                }
            }
        });

    }

    class DynamicsViewHolder extends BaseViewHolder {

        public LinearLayout llParent;
        public RoundedImageView rivHeadImg;
        public TextView tvNick;
        public TextView tvTime;
        public final TextView tvContent;
        public TextView tvShowHide;
        public WrapHeightGridView gvPics;
        public TextView tvLocation;
        public ImageView ivComment;
        public TextView tvComment;
        public ImageView ivShare;
        public ImageView ivLike;
        public TextView tvLike;
        public View divider;

        public DynamicsViewHolder(View itemView) {
            super(itemView);
            llParent = getView(R.id.ll_item_dynamics_list_parent);
            rivHeadImg = getView(R.id.riv_dynamics_head);
            tvNick = getView(R.id.tv_dynamics_nick);
            tvTime = getView(R.id.tv_dynamics_time);
            tvContent = getView(R.id.tv_dynamics_content);
            tvShowHide = getView(R.id.tv_dynamics_show_hide);
            gvPics = getView(R.id.gv_dynamics_pic);
            tvLocation = getView(R.id.tv_dynamics_location);
            ivComment = getView(R.id.iv_dynamics_comment);
            tvComment = getView(R.id.tv_dynamics_comment_num);
            ivShare = getView(R.id.iv_dynamics_share);
            ivLike = getView(R.id.iv_dynamics_is_like);
            tvLike = getView(R.id.tv_dynamics_like_num);
            divider = getView(R.id.v_divider);
        }
    }

}
